<script lang="ts" setup>
//引用区
import {reactive, ref} from "vue";
import {orderderail, orderList} from "../../api/order";
import {normalizeHms} from "../../filters";
import {useRouter} from "vue-router";
//定义区
const router = useRouter()
let totals = ref(0)
let tableData = ref([])
let dialog = ref(false)
let loading = ref(false)
let form: any = ref({})
let searchForm: any = reactive({})
const formInline: any = reactive({
  orderNo: '',
  consignee: '',
  orderState: '',
  date: ''
})
const pagin = reactive({
  currentPage: 1,
  pageSize: 3
})

//方法区
async function getData(searchFrom = {}) {
  let res = await orderList({...pagin, ...searchFrom})
  let {data, total} = res.data
  if (data.length == 0 && pagin.currentPage != 1) {
    pagin.currentPage -= 1
    getData()
    return
  }
  tableData.value = data
  totals.value = total
}

async function handleClick(row: any) {
  let res = await orderderail({id: row.id})
  let {data} = res.data
  form.value = {...data}
}

function handleSizeChange(val: any) {
  pagin.pageSize = val
  let date: any = JSON.stringify(searchForm.date)
  getData({...searchForm, date})
}

function handleCurrentChange(val: any) {
  pagin.currentPage = val
  let date: any = JSON.stringify(searchForm.date)
  getData({...searchForm, date})
}

function onSubmit() {
  let flag: any = Object.keys(formInline).some((v: any) => {
    return formInline[v]
  })
  if (!flag) return
  pagin.currentPage = 1
  searchForm = {...formInline}
  let date: any = JSON.stringify(searchForm.date)
  getData({...searchForm, date})
}

function edit(row: any) {
  const data = JSON.stringify(row)
  router.push({path: '/order/order-edit', query: {id: data}})
}
getData()
</script>
<template>
  <div id="order">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <!-- 订单号 -->
        <el-form-item label="订单号">
          <el-input
              v-model="formInline.orderNo"
              placeholder="订单号"
          ></el-input>
        </el-form-item>
        <!-- 收货人 -->
        <el-form-item label="收货人">
          <el-input
              v-model="formInline.consignee"
              placeholder="收货人"
          ></el-input>
        </el-form-item>
        <!-- 手机号 -->
        <el-form-item label="手机号">
          <el-input v-model="formInline.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <!-- 订单状态 -->
        <el-form-item label="订单状态">
          <el-select v-model="formInline.orderState" placeholder="订单状态">
            <el-option label="已完成" value="已完成"></el-option>
            <el-option label="进行中" value="进行中"></el-option>
            <el-option label="派送中" value="派送中"></el-option>
          </el-select>
          <!-- 选择时间 -->
        </el-form-item>
        <el-form-item label="选择时间">
          <el-date-picker
              v-model="formInline.date"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <!-- 查询 -->
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      <!--表格-->
      <el-table :data="tableData" border style="width: 100%">
        <!-- 订单号 -->
        <el-table-column fixed prop="orderNo" label="订单号" width="150">
        </el-table-column>
        <!-- 下单时间 -->
        <el-table-column label="下单时间" width="120">
          <template #default="scope">
            {{ normalizeHms(scope.row.orderTime) }}
          </template>
        </el-table-column>
        <!-- 手机号 -->
        <el-table-column prop="phone" label="手机号" width="120">
        </el-table-column>
        <!-- 收货人 -->
        <el-table-column prop="consignee" label="收货人" width="120">
        </el-table-column>
        <!-- 配送地址 -->
        <el-table-column prop="deliverAddress" label="配送地址" width="300">
        </el-table-column>
        <!-- 送达时间 -->
        <el-table-column label="送达时间" width="120">
          <template #default="scope">
            {{ normalizeHms(scope.row.deliveryTime) }}
          </template>
        </el-table-column>
        <!-- 用户备注 -->
        <el-table-column prop="remarks" label="用户备注" width="120">
        </el-table-column>
        <!-- 订单金额 -->
        <el-table-column prop="orderAmount" label="订单金额" width="120">
        </el-table-column>
        <!-- 订单状态 -->
        <el-table-column prop="orderState" label="订单状态">
        </el-table-column>
        <!-- 操作 -->
        <el-table-column fixed="right" label="操作" width="100">
          <template #default="scope">
            <!-- 查看 -->
            <el-button
                @click="handleClick(scope.row);dialog = true;"
                type="text"
                size="small"
            >查看
            </el-button>
            <!-- 编辑 -->
            <el-button type="text" size="small" @click="edit(scope.row)"
            >编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagin.currentPage"
          :page-sizes="[3, 5, 10]"
          :page-size="pagin.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totals"
      >
      </el-pagination>
    </el-card>
    <!--抽屉-->
    <el-drawer
        title="订单管理编辑"
        :before-close="handleClose"
        v-model="dialog"
        direction="rtl"
        custom-class="demo-drawer"
        ref="drawer"
    >
      <div class="demo-drawer__content">
        <el-form :model="form" label-width="120px">
          <el-form-item label="订单号:">
            {{ form.orderNo }}
          </el-form-item>
          <el-form-item label="下单时间:">
            {{ normalizeHms(form.orderTime) }}
          </el-form-item>
          <el-form-item label="联系电话:">
            {{ form.phone }}
          </el-form-item>
          <el-form-item label="收货人:">
            {{ form.consignee }}
          </el-form-item>
          <el-form-item label="送货地址:">
            {{ form.deliverAddress }}
          </el-form-item>
          <el-form-item label="送达时间:">
            {{ normalizeHms(form.deliveryTime) }}
          </el-form-item>
          <el-form-item label="备注:">
            {{ form.remarks }}
          </el-form-item>
          <el-form-item label="订单金额:">
            {{ form.orderAmount }}
          </el-form-item>
          <el-form-item label="订单状态:">
            {{ form.orderState }}
          </el-form-item>
          <el-form-item label-width=""></el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>
<style lang="scss" scoped>
#order {
  width: 100% !important;
}
</style>